package com.robot.merchant.controller;

import com.alibaba.fastjson.JSON;
import com.robot.merchant.client.OrdersClient;
import com.robot.merchant.pojo.Store;
import com.robot.merchant.service.StoreService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lin
 */
@RestController
@CrossOrigin
@RequestMapping("/orders")
public class OrdersController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private StoreService storeService;

    @Autowired
    private OrdersClient ordersClient;

    @Autowired
    private HttpServletRequest request;
    /**
     * store表status状态 0-禁用 1-启用
     */
    private final String storeStatusEnable = "1";

    /**
     * 根据storeId查找今天订单
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("findTodayOrders/{page}/{size}")
    public Result findOrdersByStoreIdAndToday(@PathVariable int page, @PathVariable int size) {
        Claims claims = (Claims) request.getAttribute("robot_merchant");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("storeId=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对商家是否存在和状态进行判断

        return new Result(StatusCode.SUCCESS_QUERY, JSON.parseObject(ordersClient.thirdFindSubOrdersByStoreIdAndToday(claims.getId(), page, size)));
    }

    /**
     * 根据storeId查找昨天订单
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("findYesterdayOrders/{page}/{size}")
    public Result findOrdersByStoreIdAndYesterday(@PathVariable int page, @PathVariable int size) {
        Claims claims = (Claims) request.getAttribute("robot_merchant");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("storeId=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对商家是否存在和状态进行判断

        return new Result(StatusCode.SUCCESS_QUERY, JSON.parseObject(ordersClient.thirdFindSubOrdersByStoreIdAndYesterday(claims.getId(), page, size)));
    }

    /**
     * 根据storeId查找最近7日订单
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("findWeekOrders/{page}/{size}")
    public Result findOrdersByStoreIdAndWeek(@PathVariable int page, @PathVariable int size) {
        Claims claims = (Claims) request.getAttribute("robot_merchant");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("storeId=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对商家是否存在和状态进行判断

        return new Result(StatusCode.SUCCESS_QUERY, JSON.parseObject(ordersClient.thirdFindSubOrdersByStoreIdAndWeek(claims.getId(), page, size)));
    }

    /**
     * orders根据storeId查找总订单
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("findTotalOrders/{page}/{size}")
    public Result findOrdersByStoreIdAndTotal(@PathVariable int page, @PathVariable int size) {
        Claims claims = (Claims) request.getAttribute("robot_merchant");
        if (claims == null) {
            return new Result(StatusCode.ERROR_TOKEN_EXPIRED);
        }
        logger.info("storeId=" + claims.getId());
        //前端调用findByToken接口检查过了,所以这里不再对商家是否存在和状态进行判断

        return new Result(StatusCode.SUCCESS_QUERY, JSON.parseObject(ordersClient.thirdFindSubOrdersByStoreIdAndTotal(claims.getId(), page, size)));
    }
}
